$(document).ready(function() {
	$('#enterprise').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
        "bServerSide" : true, //是否启动服务器端数据导入
        "bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
        "bJQueryUI" : true, //是否使用 jQury的UI theme
        "aLengthMenu" : [5, 10, 15], //更改显示记录数选项
        "iDisplayLength" : 10, //默认显示的记录数
        "bAutoWidth" : true, //是否自适应宽度 update\
        "bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
        "bPaginate" : true, //是否显示（应用）分页器
        "bInfo" : true, //是否显示页脚信息，DataTables插件左下角显示记录数
        "sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
        "bSort" : true, //是否启动各个字段的排序功能
        "aaSorting" : [[0, "DESC"]], //默认的排序方式，第3列，升序排列
        "bFilter" : false, //是否启动过滤、搜索功能
        "sScrollX": "100%",
		"sScrollXInner": 1695,
        "sPaginationType": "full_numbers",
	    "sAjaxSource":parent.basePath+"sys/Loan/list",
        "fnServerData":retrieveData,  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "id",
			"sTitle" : "序号",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "loanCode",
			"sTitle" : "申请号",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				return '<a href="javascript:void(0);" onclick="loan_query('+row.id+','+row.clientId+');" class="tablelink" >'+data+'</a>';
		     }
		}, {
			"mDataProp" : "loanBorrower",
			"sTitle" : "借款人",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "clientCardId",
			"sTitle" : "证件号码",
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "loanApplyDate",
			"sTitle" : "申请日期",
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {
				var value = '';
				if(_is_empty(data)){
					value = '';
				}else{
					value = new Date(data).format('yyyy-MM-dd');
				}
				
	            return value;
		     }
		}, {
			"mDataProp" : "loanLimitApply",
			"sTitle" : "申请额度(元)",
			"sDefaultContent" : "",
			"sClass" : "center",
		}, {
			"mDataProp" : "finalLimit",
			"sTitle" : "终审额度(元)",
			"sDefaultContent" : "",
			"sClass" : "center"
			
		},  {
			"mDataProp" : "finalDeadline",
			"sTitle" : "终审期限",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
			
		},{
			"mDataProp" : "loanStatus",
			"sTitle" : "贷款状态",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender": function (data, display, row) {	
				var returnStr = row.returnStatus==1?"(退回)":"(正常)";
	            return parent._local_loan_status[data]+returnStr||'异常';
		     }
		},
		  {
			"mDataProp" : "uid",
			"sTitle" : "操作",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"sWidth":"15%",
			"mRender": function (data, display, row) {
		      var id = row.id,loanStatus=row.loanStatus,clientId=row.clientId;
		      return parent._set_href(loanStatus,[id,clientId]);
		    }
			
		}
		
		],
		"oLanguage": {
            "sUrl":parent.basePath+"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
        }
        
	});
	
	//新增
	$('li._class_btn_add').click(function(){
		parent._do_goto_right(parent.basePath + 'jsp/loan/applyLoanAdd.jsp');
	});
	
	//批量删除事件
	$('li._class_btn_del').click(function(){
		var ids = _get_all_select_value();
		if(ids.length>0){
			var conf = {'tip_title':'提示信息','tip_content':'删除不可恢复，确定删除？'};
			_set_dailog(conf,function(){
				_del_data(parent.basePath +'sys/Loan/delete',{pks:ids},function(){
					$('#enterprise').dataTable().fnDraw();
				});
			});
		}
		_set_open();
	});
	
	//查询事件
	$('input[name="query_btn"]').click(function(){
		$('#enterprise').dataTable().fnDraw();
	});
	
	$('#loanStatus').mSelect({
		//myClass:'',
		width:100,
		baseCode:'Loan_Status_Code'
		//isAllSelect:false,
		//dataOptions:'required:true'
	});
	
	
});


//
var loan_del = function(id){
	$.dialog.confirm('删除不可恢复，确定删除？', function(){
		$.ajax({
	       	url:'sys/Loan/delete',
	    	data:{pks:id},
	    	type:'post',
	    	dataType:'json',
	    	success:function(result){
	    	        if(result.state=="200"){
	    	        	$.dialog({
	    					icon: 'success.gif',
	    					title: '提示',
	    				    content: '删除成功！',
	    				    ok: function(){
	    				        this.close();
	    				    }
	    				});
	    	        	$('#enterprise').dataTable().fnDraw();
	    	        }else{
	    	        	$.dialog.alert('删除失败！');
	    	        }
	    		}
	        });
	}, function(){
	});
};

var loan_edit = function(id){
	parent._do_goto_right(parent.basePath+'jsp/loan/applyLoanAdd.jsp?id='+id);
};

var loan_process = function(id){
	$.dialog({
		title:"贷款进度",
	    width: 1000,
	    height: 500,
	    content: 'url:jsp/loan/loan_process.jsp?id='+id
	});
};

var loan_stop = function(id){
	
	$.dialog.confirm('确定要中止贷款吗?', function(){
		$.ajax({
	       	url:'sys/LoanExt/stop',
	    	data:{pks:id},
	    	type:'post',
	    	dataType:'json',
	    	success:function(result){
	    	        if(result.state=="200"){
	    	        	$.dialog({
	    					icon: 'success.gif',
	    					title: '提示',
	    				    content: '中止成功！',
	    				    ok: function(){
	    				        this.close();
	    				    }
	    				});
	    	        	$('#enterprise').dataTable().fnDraw();
	    	        }else{
	    	        	$.dialog.alert('中止失败！');
	    	        }
	    		}
	        });
	}, function(){
	});
	
};


var loan_query = function(id,clientId){
	parent._do_goto_right(parent.basePath+'jsp/loan/loan_detail.jsp?id='+id+'&clientId='+clientId);
};


	
//自定义数据获取函数
function retrieveData( sSource, aoData, fnCallback ) {
		var array = $("#_form_search_default").serializeArray();
		//aoData.push({name:'ownerSql',value:'(id=2 or id=5)'})
		aoData = parent._set_query_conditions(aoData,array);
	        $.ajax( {
	                type: "POST", 
	                url: sSource, 
	                cache : false,
	                dataType : "json",
	                data : aoData, 
	                success: function(resp) {
	                	if(resp && resp.state == 200){
	                		fnCallback(resp.datagrid);
	                		return false;
	                	}
	                	fnCallback(null);
	            }
	        });
}